<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title></title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<script src="js/jquery-2.1.4.min.js"></script>
  </head>
  
  <body>
    <form action="" method="post" id="frm">
    	<table style="margin:30px 0px 0px 110px">
    		<tr>
    			<td>登录账号:</td><td><input type="text" name="username" id="username" onblur="checkName(this)" /></td>
    			<td><span id="sp2"></span></td>
    		</tr>
    		<tr>
    			<td>登录密码:</td><td><input type="password" name="psw" id="psw" onblur="checkPsw(this)"/></td>
    			<td><span id="sp3"></span></td>
    		</tr>
    		<tr>
    			<td>确认密码:</td><td><input type="password" name="password" id="password" onblur="checkPassword(this)"/></td>
    			<td><span id="sp4"></span></td>
    		</tr>
    		<tr>
    			<td>用户类型:</td>
    			<td>
    				<select name="gid">
    					<c:forEach var="sn" items="${list }">
    						<option value="${sn.gid }">${sn.gname }</option>
    					</c:forEach>
    				</select>
    			</td>
    			<td><span id="sp6"></span></td>
    		</tr>
    		<tr>
    			<td style="padding-left: 110px" colspan="10">
    				<input type="button" value="注册" onclick="register()" />
    			</td>
    		</tr>
    	</table>
    </form>
  </body>
  <script type="text/javascript">
  	function check(){
  		if(checkName()&checkPsw()){
  			return true;
  		}else {
  			return false;
  		}
  	}
  	function register(){
  		var flag = check();
  		$.ajax({
				url:'user/saveUser',
				type:'POST',
				async:true,
				data:$("#frm").serialize(),
				success:function(data){
					
				},
				error:function(){
			
				},
				complete:function(){
					var index = parent.layer.getFrameIndex(window.name);
		 		    window.parent.layer.close(index);
				}
			});
  	}
  	function checkName(){
  		var username = $("#username").val();
  		if(username==""){
  			$("#sp2").html("<font color='red'>登录账号不能为空</font>");
  			return false;
  		}else{
  			$.post("user/checkUsername","username="+username,function(data){
  	  			if(data.flag){
  	  				$("#sp2").html("<font color='green'>登录账号Ok</font>");
  	  				return true;
  	  			}else {
  	  				$("#sp2").html("<font color='red'>登录账号已存在</font>");
  	  				return false;
  	  			}
  	  		});
  		}
  		/* if(username==""){
  			$("#sp2").html("<font color='red'>登录账号不能为空</font>");
  			return false;
  		}else if(username.length!=7){
  			$("#sp2").html("<font color='red'>账号必须为7位</font>");
  			return false;
  		}else {
  			$("#sp2").html("<font color='green'>登录账号Ok</font>");
  			return true;
  		} */
  	}
  	function checkPsw(){
  		var psw = $("#psw").val();
  		if(psw==""){
  			$("#sp3").html("<font color='red'>登录密码不能为空</font>");
  			return false;
  		}else{
  			$("#sp3").html("<font color='green'>登录密码Ok</font>");
  			return true;
  		}
  	}
  	function checkPassword(){
  		var password = $("#password").val();
  		var psw = $("#psw").val();
  		if(password==""){
  			$("#sp4").html("<font color='red'>密码不能为空</font>");
  			return false;
  		}else if(password!=psw){
  			$("#sp4").html("<font color='red'>两次密码不一致</font>");
  			return false;
  		}else{
  			$("#sp4").html("<font color='green'>确认密码Ok</font>");
  			return true;
  		}
  	}
  </script>
</html>
